<template>
  <div>
    <Header :left="true" leftText="返回" title="特采会签" />

    <!-- 滑动区域，适应手机分辨率 -->
    <div class="textScroll">  
      <!-- 文字信息 -->
      <van-row style="margin-top: 10px">
        <van-col>
          <van-image
            width="40"
            height="30"
            style="margin-left: -10px"
            src="assets/block.png"
          />
        </van-col>
        <van-col class="message" style="margin-top: 5px">特采单据信息</van-col>
      </van-row>

      <div class="divMessage">
        <Field
          v-model="form.Id"
          :readonly="true"
          size="small"
          class="Field"
          center
          colon
          label="特采单ID"
        />
        <Field
          v-model="form.Description"
          rows="1"
          autosize
          style="font-weight: bold; color: #a9a9a9"
          :readonly="true"
          label="特采单描述"
          type="textarea"
        />
        <Field
          v-model="form.POrderID"
          :readonly="true"
          size="small"
          class="Field"
          center
          colon
          label="生产订单"
        />
        <Field
          v-model="form.MaterialID"
          :readonly="true"
          size="small"
          class="Field"
          center
          colon
          label="物料编号"
        />
        <Field
          v-model="form.MaterialName"
          :readonly="true"
          size="small"
          class="Field"
          center
          colon
          label="物料名称"
        />
        <Field
          v-model="form.MaterialSpec"
          :readonly="true"
          size="small"
          class="Field"
          center
          colon
          label="物料规格"
        />
        <Field
        v-model="form.BatchID"
          readonly
          size="small"
          class="Field"
          center
          colon
          label="批次号"
        >
        </Field>
        <Field
          v-model="form.Amount"
          readonly
          size="small"
          class="Field"
          center
          colon
          label="报废数量"
        />
        <Field
        readonly
          label="备注信息"
          v-model="form.Memo"
          size="small"
          class="Field"
          center
          colon
        />
        <Field
          label="问题描述"
          v-model="form.Problems"
          size="small"
          class="Field"
          center
          colon
          readonly
        />
        <Field
          size="small"
          class="Field"
          center
          colon
          label="部门"
          v-model="form.DepartmentName"
          readonly
        />
        <Field
        size="small"
        class="Field"
        center
        colon
        label="分析人签名"
        v-model="form.ReasonMan"
        readonly
      />
      <Field
        size="small"
        class="Field"
        center
        colon
        label="原因分析"
        v-model="form.Reasons"
        readonly
        />
        <Field
        size="small"
        class="Field"
        center
        colon
        label="改善措施"
        v-model="form.Advice"
        readonly
        />
        <Field
          v-model="form.Requestor"
          readonly
          size="small"
          class="Field"
          center
          colon
          label="申请人"
        />
        <Field
          v-model="form.CreateDatetime"
          readonly
          size="small"
          class="Field"
          center
          colon
          label="创建时间"
        />
      </div>
      <div v-if="isChecked == 0">
        <!-- 生产主管会签 -->
        <div v-if="status == 1">
          <van-row style="margin-top: 10px">
            <van-col>
              <van-image
              width="40"
              height="30"
              style="margin-left: -10px"
              src="assets/block.png"
              />
            </van-col>
            <van-col class="message" style="margin-top: 5px">生产主管会签</van-col>
          </van-row>
            <Field
            class="formstyle"
            v-model="form.PrdComfirmMemo"
            required
            name="处置建议"
            label="处置建议"
            placeholder="输入生产主管处置建议"
            rows="1"
            autosize
            type="textarea"
            maxlength="100"
            show-word-limit
            />
            <van-field
            required
            readonly
            class="formstyle"
            label="审核时间"
            :value="nowDate"
            />
        </div>
        <!-- 技术部主管审核 -->
        <div v-if="status == 2">
          <van-row style="margin-top: 10px">
            <van-col>
              <van-image
                width="40"
                height="30"
                style="margin-left: -10px"
                src="assets/block.png"
              />
            </van-col>
            <van-col class="message" style="margin-top: 5px">技术部主管会签</van-col>
          </van-row>         
          <Field
            class="formstyle"
            v-model="form.TechComfirmMemo"
            required
            name="处置建议"
            label="处置建议"
            placeholder="输入技术部主管处置建议"
            rows="1"
            autosize
            type="textarea"
            maxlength="100"
            show-word-limit
          />     
          <van-field
            required
            readonly
            class="formstyle"
            label="审核时间"
            :value="nowDate"
          />
        </div>
      <!-- 制程审核 -->
      <div v-if="status == 3">
        <van-row style="margin-top: 10px">
          <van-col>
            <van-image
              width="40"
              height="30"
              style="margin-left: -10px"
              src="assets/block.png"
            />
          </van-col>
          <van-col class="message" style="margin-top: 5px">品管主任会签</van-col>
      </van-row>        
        <Field
          class="formstyle"
          v-model="form.QAComfirmMemo"
          required
          name="处置建议"
          label="处置建议"
          placeholder="输入品管主任处置建议"
          rows="2"
          autosize
          type="textarea"
          maxlength="100"
          show-word-limit
        />
        <van-field
          required
          readonly
          class="formstyle"
          label="审核时间"
          :value="nowDate"
        />
      </div>
      <!-- 质量负责人审核 -->
      <div v-if="status == 4">
        <van-row style="margin-top: 10px">
          <van-col>
            <van-image
              width="40"
              height="30"
              style="margin-left: -10px"
              src="assets/block.png"
            />
          </van-col>
          <van-col class="message" style="margin-top: 5px">质量负责人审核</van-col>
        </van-row>        
        <Field
          class="formstyle"
          v-model="form.ManComfirmMemo"
          required
          name="处置建议"
          label="处置建议"
          placeholder="输入质量负责人处置建议"
          rows="2"
          autosize
          type="textarea"
          maxlength="100"
          show-word-limit
        />
        <van-field
          required
          readonly
          class="formstyle"
          label="审核时间"
          :value="nowDate"
        />
      </div>
      <!-- 利润点负责人审核 -->
      <div v-if="status == 5">
        <van-row style="margin-top: 10px">
          <van-col>
            <van-image
              width="40"
              height="30"
              style="margin-left: -10px"
              src="assets/block.png"
            />
          </van-col>
          <van-col class="message" style="margin-top: 5px">利润点负责人审核</van-col>
        </van-row>       
          <Field
            class="formstyle"
            v-model="form.GManComfirmMemo"
            required
            name="处置建议"
            label="处置建议"
            placeholder="输入利润点负责人处置建议"
            rows="2"
            autosize
            type="textarea"
            maxlength="100"
            show-word-limit
          />
          <van-field
            required
            readonly
            class="formstyle"
            label="审核时间"
            :value="nowDate"
          />
        </div>
       <div style="margin: 16px">
          <van-button
            color="#138087"
            round
            block
            type="info"
            native-type="submit"
            @click="submitCheck()"
            >确认提交</van-button
          >
        </div>    
      </div>     
    </div>
  </div>
</template>
<script>
import {
  Icon,
  NoticeBar,
  Button,
  Row,
  Tag,
  Checkbox,
  Pagination,
  popup,
  Loading,
  Popup,
  Panel,
  ImagePreview,
  Toast,
  tab,
  tabs,
  Divider,
  Image as VanImage,
  Field,
  CellGroup,
  Cell,
  Picker,
  Uploader,
  Dialog,
  Col,
  RadioGroup,
  Radio,
} from "vant";
import { _httpClient as _httpClientApi} from "@/libs/api";
import { _httpClient } from "@/libs/apisie";
import { storage } from "@/libs/utils";
import {
  removeToken,
  getToken,
  setBaseApiFrefix,
  getBaseApiFrefix,
} from "@/libs/auth";
Vue.use(Cell);
Vue.use(CellGroup);
Vue.use(tab);
Vue.use(tabs);
Vue.use(Button);
Vue.use(Col);
Vue.use(Row);
Vue.use(Icon);
Vue.use(VanImage);
Vue.use(Divider);
Vue.use(Checkbox);
Vue.use(Pagination);
Vue.use(Panel);
Vue.use(Dialog);
Vue.use(Picker);
Vue.use(popup);
Vue.use(Uploader);
Vue.use(Loading);
Vue.use(Field);
Vue.use(RadioGroup);
Vue.use(Radio);
import Header from "@/components/header.vue";
import apicloud from "@/libs/apicloud";
export default {
  name: "QCmachineconfirmItem",
  components: {
    Button,
    NoticeBar,
    Header,
    ImagePreview,
    Icon,
    Row,
    tab,
    popup,
    Popup,
    tabs,
    Tag,
    VanImage,
    Toast,
    Divider,
    Field,
    CellGroup,
    Cell,
    Uploader,
    Picker,
    Dialog,
    Col,
    RadioGroup,
    Radio,
  },

  data() {
    return {
      btnLoading: false,
      winName: "", //窗口名
      user: [], //当前登录用户
      resonShow: false, //暂停原因录入弹窗是否显示
      faultShowPicker: false, //故障原因下拉框
      screenWidth: 0,
      checked: true,
      active: 2,
      form: [],
      faultTypes: [],
      stopReson: [],
      columnsDealType:[],
      columnsQProblem:[],
      showPickerQProblem: false,
      item:{},
      valuepDealTypeName:"",  
      time:"",
      valuepShiftName: "",
      CheckTypeName: "",
      QProblemName: "",
      valueteamid: "",
      nowDate:"",
      //当前状态
      status:"",
      isChecked:false,
    };
  },
  created: function () {
    let oldHight = api.winWidth;
    this.screenWidth = oldHight;
    this.user = JSON.parse(storage.get("user"));
    this.user.userId = this.user.userName;
    //页面赋值
    this.form = api.pageParam.item;
    this.isChecked = api.pageParam.status;
    this.nowDate = this.getNowTime();
    this.initList();
    this.getQualityProblems();
    this.getshift();  
    //获取员工列表
   this.getEmployeeID();
  },
  destroyed(){       
    this.nowDate = null;
  },
  methods: {
    initList(){    
      //若未审批
      if(!this.isChecked){   
        //判断审批流程状态
        if(!this.form.PrdComfirmStatus){
          //生产主管未审核
          this.status = 1;
          this.form.PrdComfirmStatus = true;
          this.form.PrdComfirmMemo = this.getNowTime() + ":" + "生产主管:"+this.user.employeeName+"拟同意特采!"  
        }else if(this.form.PrdComfirmStatus && !this.form.TechComfirmStatus ){//生产主管审核,技术部主管未审核
          //技术部主管审核
          this.status = 2;
          this.form.TechComfirmStatus = true;
          this.form.TechComfirmMemo = this.getNowTime() + ":" + "技术部主管:"+this.user.employeeName+"拟同意特采!"
        }else if(this.form.TechComfirmStatus &&  !this.form.QAComfirmStatus){//技术部已审核，质量制程未审批
          //质量制程审核
          this.status = 3;
          this.form.QAComfirmStatus = true;
          this.form.QAComfirmMemo = this.getNowTime() + ":" + "品管主任:"+this.user.employeeName+"拟同意特采!"
        }else if(this.form.QAComfirmStatus && !this.form.ManComfirmStatus){//质量制程已审核 质量负责人未审核
          //质量负责人审核
          this.status = 4;
          this.form.ManComfirmStatus = true;
          this.form.ManComfirmMemo = this.getNowTime() + ":" + "质量负责人:"+this.user.employeeName+"拟同意特采!"
        }else if(this.form.ManComfirmStatus && !this.form.GManComfirmStatus){//质量负责人审核且同意 利润点未审批
          //利润点负责人审批
          this.status = 5;
          this.form.GManComfirmStatus = true;
          this.form.GManComfirmMemo = this.getNowTime() + ":" + "利润点负责人:"+this.user.employeeName+"拟同意特采!"
        }else{
          //意外情况
          this.status = 6;
        }
      }      
      
    },
    //提交审批
    submitCheck(){
      var body = {};
      body.Id = this.form.Id;
      //判断状态
      if(this.status == 1){       
        body.PrdComfirmMan = this.user.userId;
        body.PrdComfirmDatetime = this.nowDate;
        body.PrdComfirmStatus = this.form.PrdComfirmStatus;
        body.PrdComfirmMemo = this.form.PrdComfirmMemo;       
      }else if(this.status == 2){
        //技术部主管审核
        body.TechComfirmDatetime = this.nowDate;
        body.TechComfirmMan = this.user.userId;
        body.TechComfirmStatus = this.form.TechComfirmStatus;
        body.TechComfirmMemo = this.form.TechComfirmMemo;
      }else if(this.status == 3){
        //质量制程审核
        body.QAComfirmStatus = this.form.QAComfirmStatus;
        body.QAComfirmMemo = this.form.QAComfirmMemo;
        body.QAComfirmDatetime = this.nowDate;
        body.QAComfirmMan = this.user.userId;    
      }else if(this.status == 4){
        //质量负责人审核
        body.ManComfirmStatus = this.form.ManComfirmStatus;
        body.ManComfirmMemo = this.form.ManComfirmMemo;
        body.ManComfirmDatetime = this.nowDate;
        body.ManComfirmMan = this.user.userId;
      }else if(this.status == 5){
        //利润点负责人审核
        body.GManComfirmStatus = this.form.GManComfirmStatus;
        body.GManComfirmMemo = this.form.GManComfirmMemo;
        body.GManComfirmDatetime = this.nowDate;
        body.GManComfirmMan = this.user.userId;
      }
       this.SaveCheckEntity(body)
    },
    async SaveCheckEntity(body){
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "请稍等...",
      });
      try {
        let res = await _httpClient.post(
          _httpClient.getApiPrefix() + "/QualitySpecialPurchaseSheet/SaveEntity",
          {
            body: body,
          }
        );
        if(res.Message != null){
          //message不为空,有报错信息，打卡失败 暂时这样判断
          Toast.clear();
          Toast.fail("保存失败,请核对信息！");
        }else{
          //打卡成功
          Toast.clear();
          Toast.success("保存成功！");
          api.sendEvent({
              name: 'reloadQS',
          })
          setTimeout(function () {
            _this.$ac.closeWin();
          }, 2000);
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    showQProblemID(QProblemID) {
      var QProblemName;
      this.columnsQProblem.forEach(item => {
        if (item.QProblemID === QProblemID) {
          QProblemName = item.QProblemName;
        }
      })
      return QProblemName;
    },
    showDealType(type) {
      var DealTypeName;
      this.columnsDealType.forEach(item => {
        if (item.DealTypeID === type) {
          DealTypeName = item.DealTypeName;
        }
      })
      return DealTypeName;
    },
    //获取缺陷原因
    async getQualityProblems(){
      let _this = this;
      try {
        let res = await _httpClient.get(
          _httpClient.getApiPrefix() + "/QualityProblems/GetList",
        );
        if (res.HttpCode === "200") {     
          let temp = res.Data.map(item =>({QProblemID: item.QProblemID, QProblemName: item.QProblemName}))   
          temp.forEach(item => {
             _this.columnsQProblem.push(item);
          })  
        }      
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    // 获取当前时间
    getNowTime() {
      let dateTime;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10
          ? "0" + new Date().getMinutes()
          : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10
          ? "0" + new Date().getSeconds()
          : new Date().getSeconds();
      dateTime = yy + "-" + mm + "-" + dd + " " + hh + ":" + mf + ":" + ss;
      return dateTime;
    },
  },
};
</script>
<style>
body {
  background-color: White;
}

.pic {
  width: 100%;
  text-align: center;
}

.clickbtn {
  width: 1;
  margin: 5px 10px;
  line-height: 45px;
  border: 1px #ccc dashed;
  text-align: center;
}

.buttonColor {
  background-color: #ff9933;
  border-color: #ff9933;
}

.buttonColor2 {
  background-color: #1076c0;
  border-color: #1076c0;
}

.message {
  font-size: 15px;
  margin-top: 5px;
  margin-left: -10px;
  font-weight: bold;
}

.divStyle {
  border: 1px solid #dcdcdc;
  border-top-width: 3px;
  border-left-width: 3px;
  margin-left: 5px;
  margin-right: 4px;
  margin-top: 4px;
}

.pagination {
  position: fixed;
  bottom: 15px;
  width: 300px;
  margin-left: 10%;
  color: rgb(0, 0, 255);
  text-align: center;
}

.divMedia {
  margin-top: 0px;
  height: 230px;
  border: solid #dcdcdc;
  margin-left: 5px;
  margin-right: 15px;
  border-top-width: 4px;
  border-left-width: 4px;
  border-right-width: 1px;
  border-bottom-width: 1px;
}

.divMedia2 {
  margin-top: 0px;
  height: 160px;
  border: solid #dcdcdc;
  margin-left: 5px;
  margin-right: 15px;
  border-top-width: 4px;
  border-left-width: 4px;
  border-right-width: 1px;
  border-bottom-width: 1px;
}

.button {
  position: fixed;
  left: 5px;
  right: 5px;
  bottom: 24px;
  width: 100%;
  height: 30px;
  font-size: 12px;
}

.divMessage {
  margin-top: 0px;
  border: solid #dcdcdc;
  margin-left: 5px;
  margin-right: 15px;
  border-top-width: 4px;
  border-left-width: 4px;
  border-right-width: 1px;
  border-bottom-width: 1px;
} 

.Field {
  font-weight: bold;
  font-size: 15px;
  color: #9c8e8e;
  height: 45px;
}

.textScroll {
  display: inline-block;
  position: fixed;
  top: 5rem;
  bottom: 0rem;
  overflow: scroll;
  right: 0;
  width: 100%;
  z-index: 1;
} 
</style>
